<?php   // for user login
session_start();
include "include/dbConfig.php";
$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];
?>

<?php
$projectName = $_REQUEST['projectName'];
$projectDescription = $_REQUEST['projectDescription'];
$projectID = $_REQUEST['projectID'];
$actionType=$_REQUEST['actionType'];


/*if (!isset($name)) 
{
echo "<h1> Error in name </h1>";
echo "<br />";
} else */
{
// connect to mysql
	echo "<h1> connecting to db</h1>";
		$mysql = mysqli_connect("$dbservername", "$dbusername", "$dbpassword");
	if(!$mysql) {
	echo "Cannot connect to database.";
	exit;
	}
echo "<h1> connecting to db</h1>";
echo "<h1> connected db</h1>";
// select the appropriate database
	$selected = mysqli_select_db($mysql, "$dbname");
if(!$selected) {
echo "Cannot select database.";
exit;
}
//For Delete
if($actionType=="Del")
{
		$query = 'delete from user_projects  where user_id = '. $user_id." and project_id='".$projectID."'" ;
}
	else{
		$query = 'select count(*) from user_projects  where user_id = '. $user_id." and project_name='".$projectName."'" ;
		
		$result = mysqli_query($mysql, $query);
		var_dump($result);
		$row = mysqli_fetch_row($result);

		$count = $row[0];

		if ($count == 0) {
			echo "New User";
			$query = "insert into user_projects(user_id,project_name,project_description) values('".$user_id."','".$projectName."','".$projectDescription."') " ;		
		}
		else
		{
			$query = "UPDATE user_projects SET project_name  = '" .$projectName. "', project_description  = '" .$projectDescription. "' where user_id = '". $user_id."' and project_name='".$projectName."'" ;
		}
	}
$result = mysqli_query($mysql, $query);
 

echo "$query\n";

if ( $result );
{
		header("Location: /OSPMS/404.php");
	}

	header("Location: /OSPMS/Project.php");

}
?>


